package com.wing.bill.service;

import com.wing.bill.controller.admin.ReportUtil;
import com.wing.bill.dao.CostCenterDao;
import com.wing.bill.dao.GetNewReportDao;
import com.wing.cabinet.utils.FindDateStatusUtil;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.LocalDateTimeUtils;
import com.wing.user.feign.UserClient;
import com.wing.user.model.response.UserVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CostCenterServiceImpl implements CostCenterService{
    @Autowired
    private CostCenterDao costCenterDao;
    @Autowired
    private UserClient userClient;
    @Override
    public JsonResult list(String param, String code, String costCenter, String startTime, String endTime) {
        HashMap hm = new HashMap();
        hm.put("title","成本中心统计");
        if(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
            //要转换的对象
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            hm.put("data",costCenterDao.list(param,start,end,code,costCenter));
        }else{
            hm.put("data",costCenterDao.list(param,ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),code,costCenter));
        }

        return JsonResult.success(hm);
    }

    @Override
    public JsonResult costCenterBodyCount(String type, String startTime, String endTime, String param, Long enterpriseId, String costCenter) {
        HashMap hm = new HashMap();
        hm.put("title","按成本中心统计人数");

        List list = new ArrayList();

        if(StringUtils.isNotBlank(type)){

            if(type.equals("0")){ // 日
                list.addAll(costCenterDao.costCenterBodyCount(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),param,enterpriseId,costCenter));
                hm.put("totalSum",costCenterDao.costCenterBodyCountTotal(enterpriseId));

            }else if(type.equals("1")){ // 周
                list.addAll(costCenterDao.costCenterBodyCount(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),param,enterpriseId,costCenter));
                hm.put("totalSum",costCenterDao.costCenterBodyCountTotal(enterpriseId));

            }else if(type.equals("2")){ // 月
                list.addAll(costCenterDao.costCenterBodyCount(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),param,enterpriseId,costCenter));
                hm.put("totalSum",costCenterDao.costCenterBodyCountTotal(enterpriseId));

            }

        }
        if(StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");

            list.addAll(costCenterDao.costCenterBodyCount(start,end,param,enterpriseId,costCenter));
            hm.put("totalSum",costCenterDao.costCenterBodyCountTotal(enterpriseId));

        }
        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }

    @Override
    public JsonResult costCenterGetMuch(String type, String startTime, String endTime, String param, String enterpriseCode, String costCenter) {
        HashMap hm = new HashMap();
        hm.put("title","谁领的最多？");
        List<HashMap> list = new ArrayList();

        if(StringUtils.isNotBlank(type)){

            if(type.equals("0")){ // 日
                list.addAll(costCenterDao.costCenterGetMuch(param,ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),enterpriseCode,costCenter));

            }else if(type.equals("1")){ // 周
                list.addAll(costCenterDao.costCenterGetMuch(param,ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),enterpriseCode,costCenter));

            }else if(type.equals("2")){ // 月
                list.addAll(costCenterDao.costCenterGetMuch(param,ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),enterpriseCode,costCenter));

            }

        }
        if(StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");

            list.addAll(costCenterDao.costCenterGetMuch(param,start,end,enterpriseCode,costCenter));

        }
        for(HashMap item : list){
            if(item.get("userId") != null){
                Long userId = (Long) item.get("userId");
                if(userId != null){
                    JsonResult<UserVO> userVOJsonResult = userClient.getUserInfoByUserId(userId);
                    if(userVOJsonResult.getData().getId() != null){
                        item.put("departmentName",userVOJsonResult.getData().getDepartmentName());
                        item.put("headerImg",userVOJsonResult.getData().getHeaderImg());
                    }
                }

            }
        }
        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }

    @Override
    public JsonResult costCenterConsumeMuch(String type, String startTime, String endTime, String param, String enterpriseCode, String costCenter) {
        HashMap hm = new HashMap();
        hm.put("title","消耗规格最多");

        List list = new ArrayList();
        Map<String, Object> hashMaps = new HashMap<>();
        if(StringUtils.isNotBlank(type)){

            if(type.equals("0")){ // 日
                list.addAll(costCenterDao.costCenterConsumeMuch(param,ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),enterpriseCode,costCenter));
                hm.put("totalSum",costCenterDao.costCenterConsumeMuchTotal(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1),enterpriseCode,costCenter));
            }else if(type.equals("1")){ // 周
                list.addAll(costCenterDao.costCenterConsumeMuch(param, ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),enterpriseCode,costCenter));
                hm.put("totalSum",costCenterDao.costCenterConsumeMuchTotal(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),enterpriseCode,costCenter));

            }else if(type.equals("2")){ // 月
                list.addAll(costCenterDao.costCenterConsumeMuch(param,ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),enterpriseCode,costCenter));
                hm.put("totalSum",costCenterDao.costCenterConsumeMuchTotal(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),enterpriseCode,costCenter));

            }

        }
        if(StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");

            list.addAll(costCenterDao.costCenterConsumeMuch(param,start,end,enterpriseCode,costCenter));
            hm.put("totalSum",costCenterDao.costCenterConsumeMuchTotal(start,end,enterpriseCode,costCenter));

        }
        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }

    @Override
    public JsonResult costCenterScrap(String type, String startTime, String endTime, String param, String enterpriseCode, String costCenter,Integer businessType) {
        HashMap hm = new HashMap();
        if(businessType == 2){
            hm.put("title","谁报修最多？");
        }else if(businessType == 3){
            hm.put("title","谁报废最多？");
        }
        List list = new ArrayList();
        Map<String, Object> hashMaps = new HashMap<>();
        if(StringUtils.isNotBlank(type)){

            if(type.equals("0")){ // 日
                list.addAll(costCenterDao.costCenterScrap(param,businessType,ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),enterpriseCode,costCenter));
                hm.put("totalSum",costCenterDao.costCenterScrapTotal(businessType,ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1), enterpriseCode,costCenter));
            }else if(type.equals("1")){ // 周
                list.addAll(costCenterDao.costCenterScrap(param,businessType,ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),enterpriseCode,costCenter));
                hm.put("totalSum",costCenterDao.costCenterScrapTotal(businessType,ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),enterpriseCode,costCenter));

            }else if(type.equals("2")){ // 月
                list.addAll(costCenterDao.costCenterScrap(param,businessType,ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),enterpriseCode,costCenter));
                hm.put("totalSum",costCenterDao.costCenterScrapTotal(businessType,ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),enterpriseCode,costCenter));

            }

        }
        if(StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");

            list.addAll(costCenterDao.costCenterScrap(param,businessType,start,end,enterpriseCode,costCenter));
            hm.put("totalSum",costCenterDao.costCenterScrapTotal(businessType,start,end,enterpriseCode,costCenter));

        }

        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }

    @Override
    public JsonResult costCenterLost(String type, String startTime, String endTime,String param, String code, String costCenter, Integer businessType) {
        HashMap hm = new HashMap();
        hm.put("title","谁报失最多？");
        List list = new ArrayList();
        if(StringUtils.isNotBlank(type)){

            if(type.equals("0")){ // 日
                list.addAll(costCenterDao.queryLostResultMC(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),code,param,costCenter,businessType));
                hm.put("totalSum",costCenterDao.queryLostResultTotal(FindDateStatusUtil.dayUtil().get(0),FindDateStatusUtil.dayUtil().get(1),code,costCenter,businessType));

            }else if(type.equals("1")){ // 周
                list.addAll(costCenterDao.queryLostResultMC(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),code,param,costCenter,businessType));
                hm.put("totalSum",costCenterDao.queryLostResultTotal(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),code,costCenter,businessType));

            }else if(type.equals("2")){ // 月
                list.addAll(costCenterDao.queryLostResultMC(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),code,param,costCenter,businessType));
                hm.put("totalSum",costCenterDao.queryLostResultTotal(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),code,costCenter,businessType));

            }

        }
        if(StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");

            list.addAll(costCenterDao.queryLostResultMC(start,end,code,param,costCenter,businessType));
            hm.put("totalSum",costCenterDao.queryLostResultTotal(start,end,code,costCenter,businessType));

        }
        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }

    @Override
    public JsonResult costCenterCheck(String type, String startTime, String endTime,String param,String enterpriseCode, String costCenter) {
        HashMap hm = new HashMap();
        hm.put("title","盘验异常排名（按成本中心）");
        List<HashMap> list = new ArrayList();

        if(StringUtils.isNotBlank(type)){

            if(type.equals("0")){ // 日
                list.addAll(costCenterDao.queryInRecords(param,ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),enterpriseCode,costCenter));

            }else if(type.equals("1")){ // 周
                list.addAll(costCenterDao.queryInRecords(param,ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),enterpriseCode,costCenter));

            }else if(type.equals("2")){ // 月
                list.addAll(costCenterDao.queryInRecords(param,ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),enterpriseCode,costCenter));

            }

        }
        if(StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");

            list.addAll(costCenterDao.queryInRecords(param,start,end,enterpriseCode,costCenter));

        }
        hm.put("data",list);
        return JsonResult.success("查询成功",hm);
    }

}
